﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmCustomer : Form
    {
        int cid = -1;
        DataSet ds;
        public frmCustomer()
        {
            InitializeComponent();
        }
        public frmCustomer(int id)
        {
            cid = id;
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string sex="男";
            if(rbtnNv.Checked==true)
               sex="女";
            string birthday = dtpBirthday.Value.ToShortDateString();
       
            bool chiyao;
            bool yundong;
            int i;
            if (chbChiYao.Checked == true)
                chiyao =true;                  
            else
                chiyao =false;
            if (chbYunDong.Checked == true)
                yundong =true;
            else
                yundong = false;
            int shenggao;
            if (txtShengGao.Text != "")
                shenggao = int.Parse(txtShengGao.Text);
            else
                shenggao =0;
            int ceshi;
                if(txtTestRate.Text!="")
                 ceshi= int.Parse(txtTestRate.Text);
               else 
                    ceshi=0;
                string sql;
          
            int  YYType=(int)cmbYingYangShi.SelectedValue;
            string stzk = "";//把身体状况情况放在该字符串中
            foreach (Control ctr in this.Controls)
            {
                if (ctr is CheckBox)
                {
                    CheckBox cb = ctr as CheckBox;

                    if (cb.Name != "chbYunDong" && cb.Name != "chbChiYao")
                    {
                        if (cb.Checked)
                        {
                            stzk += cb.Text+",";
                        }

                    }
                }
            }
            if(stzk.Length>0)//不为空时
           stzk= stzk.Substring(0, stzk.LastIndexOf(","));
            if (txtName.Text == "" || txtTel.Text == "")
            {
                MessageBox.Show("请输入客户的姓名和电话！");
            }
            else
            {
                if (cid == -1)//新建客户信息
                {

                    sql = string.Format("INSERT INTO customer ( [ctype] ,[cname] ,[cpinyin] ,[csex] ,[cbirthday] ,[cphone] ,[ccalltime] ,[caddress] ,[ctestrate] ,[cheight] ,[cbodystate] ,[did] ,[cwhy] ,[cintention1] ,[cintention2] ,[cintention3] ,[cispill] ,[cissport] ,[ceat] ,[creference] )values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},'{10}',{11},'{12}','{13}','{14}','{15}',{16},{17},'{18}','{19}')",
                                                               cmbType.Text, txtName.Text, txtNamePinyin.Text, sex, birthday, txtTel.Text, txtCallTime.Text, txtAddress.Text, ceshi, shenggao, stzk, YYType, txtWhy.Text, txtDongji1.Text, txtDongji2.Text, txtDonJi3.Text, chiyao, yundong, txtChiFan.Text, txtJieShao.Text);


                    i = DbHelperOleDb.ExecuteSql(sql);
                    if (i >= 1)
                    {
                        MessageBox.Show("添加客户成功！");
                        this.Hide();
                        frmCustomer_Load(null, null);
                    }
                    else
                    {
                        MessageBox.Show("添加客户失败！");
                    }
                }
                else//修改客户信息
                {
                    sql = "select  from customer where cid=" + cid;


                    sql = string.Format(@"update customer set   [creference] = '{0}', [ctype] ='{1}',[cname] = '{2}',[cpinyin] = '{3}',[csex] = '{4}',[cbirthday] =#{5}#,[cphone] = '{6}',[ccalltime] = '{7}',[caddress] = '{8}',[ctestrate] ={9},[cheight] = {10},
                                             [cbodystate] = '{11}',[did] = {12},[cwhy] = '{13}',[cintention1] = '{14}',[cintention2] = '{15}',[cintention3] = '{16}',[cispill] = {17},[cissport] = {18},[ceat] = '{19}' where cid={20}",
                                                 txtJieShao.Text, cmbType.Text, txtName.Text, txtNamePinyin.Text, sex, birthday, txtTel.Text, txtCallTime.Text, txtAddress.Text, ceshi, shenggao, stzk, YYType, txtWhy.Text, txtDongji1.Text, txtDongji2.Text, txtDonJi3.Text, chiyao, yundong, txtChiFan.Text, cid);//19后面多个，

                    i = DbHelperOleDb.ExecuteSql(sql);
                    if (i == 1)
                    {
                        MessageBox.Show("修改客户信息成功！");
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("修改客户信息失败！");
                    }
                }
            }

                
        }

        private void frmCustomer_Load(object sender, EventArgs e)
        {
            cmbType.Text = "客户";
            string sql = "select * from dietitianinfo";
            ds = DbHelperOleDb.Query(sql);
            cmbYingYangShi.DataSource = ds.Tables[0];
            cmbYingYangShi.DisplayMember = "dName";
            cmbYingYangShi.ValueMember = "did";
            if (!userinfo.isadmin)
                cmbYingYangShi.SelectedValue = userinfo.id;
            if (cid != -1)
            {
                sql = "select * from customer where cid=" + cid;
                ds=DbHelperOleDb.Query(sql);
               //根据字符分割符号“，”把数据库中的身体状况情况分开
                string[] word = ds.Tables[0].Rows[0]["cbodystate"].ToString().Split(new char[] { ',' });
                
                foreach (string s in word)
                {  
                    foreach (Control ctr in this.Controls)
                    {   
                        if (ctr is CheckBox)
                        {
                            CheckBox cb = ctr as CheckBox;

                            if (cb.Name != "chbYunDong" && cb.Name != "chbChiYao")
                            {
                                if (Equals(cb.Text,s))
                                {
                                    cb.Checked = true;
                                }

                            }
                        }
                    }
                }

                if (ds.Tables[0].Rows.Count == 1)
                {//出现要修改客户的信息
                    txtName.Text = ds.Tables[0].Rows[0]["cName"].ToString();
                    txtNamePinyin.Text = ds.Tables[0].Rows[0]["cpinyin"].ToString();
                    txtTel.Text = ds.Tables[0].Rows[0]["cphone"].ToString();
                    txtAddress.Text = ds.Tables[0].Rows[0]["caddress"].ToString();
                    txtCallTime.Text = ds.Tables[0].Rows[0]["ccalltime"].ToString();
                    txtChiFan.Text = ds.Tables[0].Rows[0]["ceat"].ToString();
                    txtDongji1.Text = ds.Tables[0].Rows[0]["cintention1"].ToString();
                    txtDongji2.Text = ds.Tables[0].Rows[0]["cintention2"].ToString();
                    txtDonJi3.Text = ds.Tables[0].Rows[0]["cintention3"].ToString();
                    txtJieShao.Text = ds.Tables[0].Rows[0]["creference"].ToString();
                    txtShengGao.Text = ds.Tables[0].Rows[0]["cheight"].ToString(); 
                    txtTestRate.Text = ds.Tables[0].Rows[0]["ctestrate"].ToString(); 
                    txtWhy.Text = ds.Tables[0].Rows[0]["cwhy"].ToString(); ;
                   //txtZhuangKuang.Text = ds.Tables[0].Rows[0]["cbodystate"].ToString();
                    dtpBirthday.Text = ds.Tables[0].Rows[0]["cbirthday"].ToString();
                    if (ds.Tables[0].Rows[0]["csex"].ToString() == "男")
                        rbtnNan.Checked = true;
                    else
                        rbtnNv.Checked = true;
                    if (ds.Tables[0].Rows[0]["cissport"].ToString()== "true")
                    {
                        chbYunDong.Checked = true;
                       
                    }
                    if (ds.Tables[0].Rows[0]["cispill"].ToString() == "true")
                    {
                        chbChiYao.Checked = true;
                    }
                    
                }
            }
            //this.Refresh();
            this.WindowState = FormWindowState.Maximized;
        }

       
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (txtName.Text != "")
                txtNamePinyin.Text = myfun.GetChineseSpell(txtName.Text);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtAddress.Text = "";
            txtCallTime.Text = "";
            txtChiFan.Text = "";
            txtDongji1.Text = "";
            txtDongji2.Text = "";
            txtDonJi3.Text = "";
            txtJieShao.Text = "";
            txtName.Text = "";
            txtNamePinyin.Text = "";
            txtShengGao.Text = "";
            txtTel.Text = "";
            txtTestRate.Text = "";
            txtWhy.Text = "";
            foreach (Control ctr in this.Controls)
            {
                if (ctr is CheckBox)
                {
                    CheckBox cb = ctr as CheckBox;

                     cb.Checked =false;
                        

                    }
                }
            }

        private void frmCustomer_Resize(object sender, EventArgs e)
        {
            //this.Refresh();
        }

      
        

      

      
        
        
    }
}
